package com.example.ordersystem;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.ordersystem.pojo.User;
import com.example.ordersystem.utils.Code;
import com.example.ordersystem.utils.ListDataSave;

import java.util.ArrayList;
import java.util.List;

/*
 * 注册页面
 */
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private Button register_btn_register;//注册按钮
    private EditText register_username;//注册用户名
    private EditText register_password1;//注册密码
    private EditText register_password2;//重复密码
    private ImageView register_back_to_login;//返回登录页面
    private String realCode;//定义一个变量接收
    private EditText register_YZCodes;//验证码
    private ImageView register_showCode;//验证码图案
    private EditText register_phone;//手机号
    private EditText register_address;//地址

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();
    }

    public void initView() {
        register_btn_register = findViewById(R.id.register_btn_register);
        register_back_to_login = findViewById(R.id.register_back_to_login);
        register_username = findViewById(R.id.register_username);
        register_password1 = findViewById(R.id.register_password1);
        register_password2 = findViewById(R.id.register_password2);
        register_YZCodes = findViewById(R.id.register_YZCodes);
        register_showCode = findViewById(R.id.register_showCode);
        register_phone = findViewById(R.id.register_editText_phone);
        register_address = findViewById(R.id.register_editText_address);

        register_back_to_login.setOnClickListener(this);
        register_btn_register.setOnClickListener(this);
        register_showCode.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register_showCode:    //改变随机验证码的生成
                register_showCode.setImageBitmap(Code.getInstance().createBitmap());
                realCode = Code.getInstance().getCode().toLowerCase();
                break;
            case R.id.register_back_to_login:
                startActivity(new Intent(this, LoginActivity.class));
                finish();
                break;
            case R.id.register_btn_register:

                String username = register_username.getText().toString().trim();
                String password1 = register_password1.getText().toString().trim();
                String password2 = register_password2.getText().toString().trim();
                String phoneCode = register_YZCodes.getText().toString().toLowerCase();
                String phone = register_phone.getText().toString().trim();
                String address = register_address.getText().toString().trim();

                if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password1) && !TextUtils.isEmpty(password2) && !TextUtils.isEmpty(phoneCode)) { //判空
                    boolean flag1 = true;

                    List<User> userList = ListDataSave.getUserList(this, username);
                    if (userList != null) {
                        for (User user : userList) {
                            if (username.equals(user.getName())) {
                                flag1 = false;

                                break;
                            } else flag1 = true;
                        }
                    }

                    if (flag1) {
                        if (password1.equals(password2)) {

                            if (phoneCode.equals(realCode)) {

                                List<User> userList1 = new ArrayList<>();

                                userList1.add(new User(username, password1, phone, address));

                                ListDataSave.putUserBean(this, userList1, username);

                                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();

                                startActivity(new Intent(this, LoginActivity.class));
                                finish();

                            } else Toast.makeText(this, "验证码有误", Toast.LENGTH_SHORT).show();

                        } else Toast.makeText(this, "两次密码不相同", Toast.LENGTH_SHORT).show();

                    } else Toast.makeText(this, "用户名已存在", Toast.LENGTH_SHORT).show();

                } else Toast.makeText(this, "用户名和密码、验证码不能为空", Toast.LENGTH_SHORT).show();

                break;
        }
    }
}
